<?php

namespace App\Common\Web;


use App\Common\Log;
use App\Common\Sys;
use App\Common\WX\WxWord;
use XWX\Common\H;
use XWX\Common\XEntity;

class CookieModel extends XEntity
{
    protected $APP__user_id;
    protected $APP__x;
    protected $APP__app_id;
    protected $APP__word;
    protected $APP__timestamp;
    protected $APP__sign;


    public function __construct($data = null)
    {
        parent::__construct($data);
    }


    public function getUserId()
    {
        return $this->APP__user_id;
    }

    public function x()
    {
        return $this->APP__x;
    }

    public function getAppId()
    {
        return $this->APP__app_id;
    }

    public function getWord()
    {
        return $this->APP__word;
    }

    public function funcGetWxWord()
    {
        $app = new WxWord($this->getWord());

        return $app;
    }


    public function getTimestamp()
    {
        return $this->APP__timestamp;
    }

    public function getSign()
    {
        return $this->APP__sign;
    }


    /**
     * 验证签名
     *
     * @param $sign_key *签名的key
     * @return bool
     */
    public function funcCheckSign($sign_key)
    {
        $list = $this->toArray();

        $sign_data = [];
        foreach ($list as $k => $v)
        {
            if (H::funcStrStartsWith($k, 'APP__') && $k != 'APP__sign')
            {
                $sign_data[$k] = $v;
            }
        }

        $sign = Sys::funcGetSign($sign_data, $sign_key);
        if ($sign == $this->getSign())
        {
            return true;
        }

        Log::funcLogDebug(__METHOD__, "{$sign},{$this->getSign()}");


        return false;
    }
}